﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

using ArkanoidX.Objects;

namespace ArkanoidX.UI
{
    class RightPanel : Window
    {
        public Texture2D Texture = null;
        public Level Level;

        public Text LevelText;
        public Text Score;
        public Text Passcode;

        public Text Lives;

        public Texture2D Racket;
        int racketWidth;
        int racketHeight;
        Vector2 racketSize;
        Vector2 racketHalfSize;
        int MaxShipsPerRow = 4;


        public RightPanel(MainGame game, Layer layer, Level level)
            : base(game)
        {
            LevelText = new Text(game);
            LevelText.Font = game.Content.Load<SpriteFont>("Levels");
            LevelText.Color = Color.DarkGray;

            Score = new Text(game);
            Score.Font = game.Content.Load<SpriteFont>("Levels");
            Score.Color = Color.Blue;

            Lives = new Text(game);
            Lives.Font = game.Content.Load<SpriteFont>("Levels");
            Lives.Color = Color.Red;

            Passcode = new Text(game);
            Passcode.Font = game.Content.Load<SpriteFont>("Levels");
            Passcode.Color = Color.Green;

            this.background = game.Content.Load<Texture2D>("PanelBg");
            boundingRect = game.Panel;

            Level = level;
        }


        public override void Update(GameTime gameTime)
        {
            MainGame game = (MainGame)base.Game;
            Rectangle rect = game.Panel;

            if (game.paused)
            {
                LevelText.Value = String.Format("Level: {0} (Paused)", Level.LevelNumber);
            }
            else
            {
                LevelText.Value = String.Format("Level: {0}", Level.LevelNumber);
            }
            LevelText.Align(rect, Window.AlignType.TopCenter, 0, 20);

            Passcode.Value = String.Format("Passcode: {0}", Level.Password);
            Passcode.Align(rect, Window.AlignType.TopCenter, 0, 50);

            Score.Value = String.Format("Score: {0}", Level.Score);
            Score.Align(rect, Window.AlignType.TopCenter, 0, 80);

            Lives.Value = String.Format("{0} Lives Left", Level.Lives);
            Lives.Align(rect, Window.AlignType.BottomCenter, 0, 20);

            if (Racket == null)
            {
                RacketStates.NormalState state = (RacketStates.NormalState)
                    Level.Racket.StateCache[typeof(RacketStates.NormalState)];
                if (state != null)
                {
                    Racket = state.Texture;

                    racketWidth = this.boundingRect.Width / 6;
                    racketHeight = racketWidth * this.Racket.Height / this.Racket.Width;
                    racketSize = new Vector2(racketWidth, racketHeight);
                    racketHalfSize = new Vector2(racketWidth, racketHeight) * 0.5f;
                }
            }

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            base.Draw(gameTime);

            MainGame game = (MainGame)base.Game;

            SpriteBatch sBatch = (SpriteBatch)Game.Services.GetService(typeof(SpriteBatch));
            //sBatch.Draw(Texture, rect, Color.White);

            // works as a sub component of this panel which manages its drawing
            LevelText.Draw(gameTime);
            Score.Draw(gameTime);
            Lives.Draw(gameTime);
            Passcode.Draw(gameTime);

            if (Racket != null)
            {
                Vector2 position = this.CalcAlign(this.boundingRect, AlignType.BottomLeft, racketSize, 20, 100);
                position -= racketHalfSize;

                int rows = (Level.Lives + 3) / MaxShipsPerRow;
                position.Y -= racketHeight * 2 * (rows - 1);
                float initialX = position.X;

                for (int i = 0; i < Level.Lives; i++)
                {
                    if (i != 0 && i % MaxShipsPerRow == 0)
                    {
                        position.Y += racketHeight * 2;
                        position.X = initialX;
                    }
                    Rectangle rrect = new Rectangle((int)position.X, (int)position.Y, racketWidth, racketHeight);
                    sBatch.Draw(this.Racket, rrect, Color.White);
                    position.X += racketWidth * 1.2f;
                }
            }
        }
    }
}
